
	#include "cGame_Ingame_InitMatch.h"
	#include "cGame_Ingame_Camera.h"
	#include "cGame_Ingame_Cursor.h"
	#include "cGame_Ingame_DrawMap.h"
	#include "cGame_Ingame_DrawPlayerInfo.h"
	#include "cGame_Ingame_Dice.h"
	#include "cGame_Ingame_HandleRound.h"
	#include "cGame_Ingame_BattleResults.h"
	#include "cGame_Ingame_Cards.h"
	#include "cGame_Ingame_Tools.h"
	#include "cGame_Ingame_Checkwin.h"
	
	public static int ingame_state = 0;
	public static int prev_ingame_state = 0;
	
	public static void SetIngameState(int new_state)
	{
		prev_ingame_state = ingame_state;
		ingame_state = new_state;
	}
		
	public void UpdateIngame()
	{
		switch(ingame_state)
		{
			case STATES.INGAME_INIT:
			
				splashImg = null;
				initMatch();
				SetIngameState(STATES.INGAME_FIRST_ROUND);
				
			break;
			
			case STATES.INGAME_LOAD_MATCH:
				UpdateLoadMatch();
			break;
			
			case STATES.INGAME_FIRST_ROUND:
			
				UpdateCursor();
				UpdateCamera();
				drawMap();
				
				drawSoftKeys(SOFTKEYS.STR_TOOLS, SOFTKEYS.STR_MENU);
				
				if(drawIngameHelp)
					drawIngameHelp();
				
				if(drawPlayerInfo)
					drawPlayerInfo();
				
				if(isKeyPressed(KEYS.KEY_FIRE) || isKeyPressed(KEYS.KEY_5))
				{
					detectCountry(cPlayer.roundType);
					clearKeys();
				}
				else if(isKeyPressed(KEYS.KEY_STAR))
				{
					if(cCountry.selectedCountry != cCountry.NONE_SELECTED && tegCountries[cCountry.selectedCountry].owner == cPlayer.turn)
					{
						tegCountries[cCountry.selectedCountry].armies++;
						tegPlayers[cPlayer.turn].armies--;
					}
					
					if(tegPlayers[cPlayer.turn].armies == 0)
					{
						cCountry.selectedCountry = cCountry.NONE_SELECTED;
						
						cPlayer.turn++;
						
						if(cPlayer.turn > rules[RULES.MATCH_PLAYERS] - 1)
						{
							cPlayer.turn -= rules[RULES.MATCH_PLAYERS];
						}
						
						shownPlayerInfo = cPlayer.turn;
						
						if(cPlayer.turn == cPlayer.firstRoundTurn)
						{							
							if(cPlayer.turn > rules[RULES.MATCH_PLAYERS] - 1)
							{
								cPlayer.turn -= rules[RULES.MATCH_PLAYERS];
							}
							
							cPlayer.firstRoundTurn = cPlayer.turn;
							shownPlayerInfo = cPlayer.turn;
								
							for(int i = 0; i < rules[RULES.MATCH_PLAYERS]; i++)
							{
								tegPlayers[i].armies = 3;
							}
							
							SetIngameState(STATES.INGAME_STANDARD_ROUND);
						}
					}
					
					clearKeys();
				}
				else if(isKeyPressed(KEYS.KEY_1))
				{
					drawIngameHelp = !drawIngameHelp;
					clearKeys();
				}
				else if(isKeyPressed(KEYS.KEY_7))
				{
					drawPlayerInfo = !drawPlayerInfo;
					clearKeys();
				}
				else if(isKeyPressed(KEYS.KEY_8) && drawPlayerInfo)
				{
					shownPlayerInfo++;
					
					if(shownPlayerInfo > rules[RULES.MATCH_PLAYERS] - 1)
					{
						shownPlayerInfo -= rules[RULES.MATCH_PLAYERS];
					}
					
					clearKeys();
				}
				else if(isKeyPressed(KEYS.KEY_LSK))
				{
					SetIngameState(STATES.INGAME_TOOLS);
					clearKeys();
				}
				else if(isKeyPressed(KEYS.KEY_RSK))
				{
					SetState(STATES.IGM);
				}
				
			break;
			
			case STATES.INGAME_STANDARD_ROUND:
			
				if(!showBattleResults && !showCard)
				{
					UpdateCursor();
					UpdateCamera();
				}
				drawMap();
				
				drawSoftKeys(SOFTKEYS.STR_TOOLS, SOFTKEYS.STR_MENU);
								
				if(drawPlayerInfo)
					drawPlayerInfo();
				
				if(drawIngameHelp)
					drawIngameHelp();
				
				if(!showBattleResults && !showCard)
				{
					if(isKeyPressed(KEYS.KEY_FIRE) || isKeyPressed(KEYS.KEY_5))
					{
						detectCountry(cPlayer.roundType);
						clearKeys();
					}
					else if(isKeyPressed(KEYS.KEY_STAR))
					{
						UpdateTurn(KEYS.KEY_STAR);						
						clearKeys();
					}
					else if(isKeyPressed(KEYS.KEY_POUND))
					{
						UpdateTurn(KEYS.KEY_POUND);
						clearKeys();
					}
					else if(isKeyPressed(KEYS.KEY_1))
					{
						drawIngameHelp = !drawIngameHelp;
						clearKeys();
					}
					else if(isKeyPressed(KEYS.KEY_6) && canPickCard)
					{
						UpdatePickCard();
						clearKeys();
					}
					else if(isKeyPressed(KEYS.KEY_7))
					{
						drawPlayerInfo = !drawPlayerInfo;
						clearKeys();
					}
					else if(isKeyPressed(KEYS.KEY_8) && drawPlayerInfo)
					{
						shownPlayerInfo++;
						
						if(shownPlayerInfo > rules[RULES.MATCH_PLAYERS] - 1)
						{
							shownPlayerInfo -= rules[RULES.MATCH_PLAYERS];
						}
						
						clearKeys();
					}
					else if(isKeyPressed(KEYS.KEY_LSK))
					{
						SetIngameState(STATES.INGAME_TOOLS);
						clearKeys();
					}
					else if(isKeyPressed(KEYS.KEY_RSK))
					{
						SetState(STATES.IGM);
						clearKeys();
					}
				}
				else // !showBattleResults
				{
					if(showBattleResults)
					{
						drawResults();
						if(!conqueredCountry)
							drawSoftKeys(SOFTKEYS.STR_CONTINUE, SOFTKEYS.STR_NONE);
						
						if(isKeyPressed(KEYS.KEY_LSK) && !conqueredCountry)
						{
							showBattleResults = false;
							clearKeys();
						}
					}
					else if(showCard)
					{
						drawCard(pickedCard);
						
						if(tegCountries[pickedCard].owner == cPlayer.turn)
						{
							drawSoftKeys(SOFTKEYS.STR_GET_ARMIES, SOFTKEYS.STR_SAVE_CARD);
							
							if(isKeyPressed(KEYS.KEY_LSK))
							{
								moveCameraTo(tegCountries[pickedCard].collisionRect[cCountry.RECT_X] + tegCountries[pickedCard].collisionRect[cCountry.RECT_W] / 2, tegCountries[pickedCard].collisionRect[cCountry.RECT_Y] + tegCountries[pickedCard].collisionRect[cCountry.RECT_H] / 2);
								showCard = false;
								tegCountries[pickedCard].armies += 2;
								tegCountries[pickedCard].usedCard = true;
								clearKeys();
							}
							else if(isKeyPressed(KEYS.KEY_RSK))
							{
								showCard = false;
								clearKeys();
							}
						}
						else
						{
							drawSoftKeys(SOFTKEYS.STR_NONE, SOFTKEYS.STR_SAVE_CARD);
							
							if(isKeyPressed(KEYS.KEY_RSK))
							{
								showCard = false;
								clearKeys();
							}
						}
					}
				}
			break;
			
			case STATES.INGAME_TOOLS:
				UpdateIngameTools();
				clearKeys();
			break;
			
			case STATES.INGAME_MATCH_END:
				UpdateWinner();
				clearKeys();
			break;
		}
	}